﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Imaging;

namespace SmartMosaic
{
    public partial class Form1 : Form
    {

        private bool isMouseDown;
        private int oldX;
        private int oldY;

        int level = 0;

        Bitmap bitmap = null;
        Image backgroundImage = new Bitmap("\\3_2.jpg");

        public Form1()
        {
            InitializeComponent();

            bitmap = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);

            this.Load += new EventHandler(Form1_Load);

            this.MouseDown += new MouseEventHandler(Form1_MouseDown);
            this.MouseUp += new MouseEventHandler(Form1_MouseUp);
            this.MouseMove += new MouseEventHandler(Form1_MouseMove);
        }

        void Form1_Load(object sender, EventArgs e)
        {
            Invalidate();
        }

        void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                int deltaX = e.X - oldX;
                int deltaY = e.Y - oldY;

                level += deltaX;

                //leftTopOfView.X += deltaX;
                //leftTopOfView.Y += deltaY;

                oldX = e.X;
                oldY = e.Y;

                Invalidate();
            }
        }

        void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;
            oldX = e.X;
            oldY = e.Y;
        }

        //private int width = 255;
        //private int height = 255;

        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);

        //    Graphics gxBuffer = Graphics.FromImage(bitmap);

        //    gxBuffer.Clear(Color.Black);


        //    Rectangle srcRect = new Rectangle(0, 0, width, height);

        //    Rectangle dstRect = new Rectangle(0, 0, 255, 255);

        //    dstRect.Location = new Point(
        //        (e.ClipRectangle.Width - dstRect.Width) / 2,
        //        (e.ClipRectangle.Height - dstRect.Height) / 2);

        //    gxBuffer.DrawImage(backgroundImage, dstRect, srcRect, GraphicsUnit.Pixel);

        //    e.Graphics.DrawImage(bitmap, 0, 0);


        //    //Image img = new Bitmap("\\test\\0_0.jpg");

        //    //Graphics graphics = e.Graphics;

        //    //graphics.DrawImage(img, dstRect, srcRect, GraphicsUnit.Pixel);
        //}

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics gxBuffer = Graphics.FromImage(bitmap);

            gxBuffer.Clear(Color.Black);





            Rectangle srcRect = new Rectangle
                (0, 0, backgroundImage.Width, backgroundImage.Height);

            Rectangle dstRect = new Rectangle(0, 0, backgroundImage.Width + level, backgroundImage.Height + level);

            //Rectangle srcRect = new Rectangle
            //    (0, 0, 256, 256);

            //Rectangle dstRect = new Rectangle(0, 0, 256, 256);

            dstRect.Location = new Point(
                (e.ClipRectangle.Width - dstRect.Width) / 2,
                (e.ClipRectangle.Height - dstRect.Height) / 2);

            gxBuffer.DrawImage(backgroundImage, dstRect, srcRect, GraphicsUnit.Pixel);


            //gxBuffer.Clip = new RectangleF(0.0f, 0.0f, 300.0f, 300.0f);

            //gxBuffer.Clip = new Region(new Rectangle(0, 0, 300, 300));

            //gxBuffer.DrawImage(backgroundImage, dstRect, 0, 0, 256, 256, GraphicsUnit.Pixel, new ImageAttributes());

            
            

            e.Graphics.DrawImage(bitmap, 0, 0);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(DoPrinting));
            thread.Start();
        }

        delegate void MyDelegate();

        private void DoPrinting()
        {
            for (int i = 0; i < 10; i++)
            {
                level = i * 50 + 256;

                

                this.BeginInvoke(new MyDelegate(Invalidate));

                Thread.Sleep(1);
            }
        }
    }
}